import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'dart:math' as math;
import 'package:my_ebook_app/common/sizes.dart';
import 'package:my_ebook_app/pages/theme_notifier.dart';
import 'package:provider/provider.dart';

class WelcomePage extends StatefulWidget {
  const WelcomePage({super.key});

  @override
  _WelcomePageState createState() => _WelcomePageState();
}

class _WelcomePageState extends State<WelcomePage> with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _animation;
  bool _isHovered = false;

  @override
  void initState() {
    super.initState();
    _loadThemeAndAnimate();
    _animationController = AnimationController(
      duration: Duration(milliseconds: 1200),
      vsync: this,
    );

    _animation = CurvedAnimation(
      parent: _animationController,
      curve: Curves.easeInOut,
    );

    _animationController.forward();
  }
  Future<void> _loadThemeAndAnimate() async {
    final themeNotifier = Provider.of<ThemeNotifier>(context, listen: false);
    await themeNotifier.loadTheme();
  }
  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }
  void _onEnter(PointerEvent event) {
    setState(() {
      _isHovered = true;
    });
  }
  void _onExit(PointerEvent event) {
    setState(() {
      _isHovered = false;
    });
  }
  void _showLoginDialog(bool isDarkMode) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Center(
            child: Text('登录'),
          ),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              TextField(
                decoration: InputDecoration(labelText: '账号'),
              ),
              TextField(
                decoration: InputDecoration(labelText: '密码'),
                obscureText: true,
              ),
            ],
          ),
          actions: [
            TextButton(
              onPressed: () {
                context.push('/home');
              },
              child: Text('确认'),
              style: TextButton.styleFrom(
                  backgroundColor: Color(0x3fe2566)
              ),
            ),
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: Text('取消'),
              style: TextButton.styleFrom(
                  backgroundColor: isDarkMode?Color(0x3fe2566):Color(0x1affffff)
              ),
            ),
          ],
        );
      },
    );
  }
  void _showRegisterDialog(bool isDarkMode) {
      showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Center(
              child: Text('注册'),
            ),
            content: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                TextField(
                  decoration: InputDecoration(labelText: '账号'),
                ),
                TextField(
                  decoration: InputDecoration(labelText: '密码'),
                  obscureText: true,
                ),
                TextField(
                  decoration: InputDecoration(labelText: '确认密码'),
                  obscureText: true,
                )
              ],
            ),
            actions: [
              TextButton(
                onPressed: () {
                  Navigator.of(context).pop();
                  _showLoginDialog(isDarkMode);
                },
                child: Text('确认'),
                style: TextButton.styleFrom(
                    backgroundColor: Color(0x3fe2566)
                ),
              ),
              TextButton(
                onPressed: () {
                  Navigator.of(context).pop();
                },
                child: Text('取消'),
                style: TextButton.styleFrom(
                    backgroundColor: isDarkMode?Color(0x3fe2566):Color(0x1affffff)
                ),
              ),
            ],
          );
        },
      );
  }
  @override
  Widget build(BuildContext context) {
    final isDarkMode = Theme.of(context).brightness == Brightness.dark;
    return Scaffold(
      body: Stack(
        children: [
          AnimatedBuilder(
            animation: _animation,
            builder: (context, child) {
              return Positioned(
                top: -360 + 180 * _animation.value,
                left: -140 + 70 * _animation.value,
                child: ConstrainedBox(
                  constraints: BoxConstraints(
                    maxWidth: 300,
                    maxHeight: 400,
                  ),
                  child: FractionallySizedBox(
                    widthFactor: 0.75,
                    heightFactor: 0.75,
                    child: Transform.rotate(
                      angle: -17 * math.pi / 18,
                      child: isDarkMode
                          ? ShaderMask(
                        shaderCallback: (Rect bounds) {
                          return LinearGradient(
                            colors: [Color(0x80000000), Color(0x80000000)],
                          ).createShader(bounds);
                        },
                        blendMode: BlendMode.darken,
                        child: Container(
                          decoration: BoxDecoration(
                            image: DecorationImage(
                              image: AssetImage('lib/common/images/welcomeBook1.jpg'),
                              fit: BoxFit.cover,
                            ),
                            boxShadow: [
                              BoxShadow(
                                color: Colors.black12,
                                blurRadius: 5,
                                spreadRadius: 2,
                              ),
                            ],
                          ),
                        ),
                      )
                          : Container(
                        decoration: BoxDecoration(
                          image: DecorationImage(
                            image: AssetImage('lib/common/images/welcomeBook1.jpg'),
                            fit: BoxFit.cover,
                          ),
                          boxShadow: [
                            BoxShadow(
                              color: Colors.black12,
                              blurRadius: 5,
                              spreadRadius: 2,
                            ),
                          ],
                        ),
                      ),
                    ),

                    ),
                  ),
                );
            },
          ),
          AnimatedBuilder(
            animation: _animation,
            builder: (context, child) {
              return Positioned(
                top: -240 + 120 * _animation.value,
                right: -280 + 140 * _animation.value,
                child: ConstrainedBox(
                  constraints: BoxConstraints(
                    maxWidth: 300,
                    maxHeight: 400,
                  ),
                  child: FractionallySizedBox(
                    widthFactor: 0.75,
                    heightFactor: 0.75,
                    child: Transform.rotate(
                      angle: -7 * math.pi / 18,
                      child: isDarkMode
                          ? ShaderMask(
                        shaderCallback: (Rect bounds) {
                          return LinearGradient(
                            colors: [Color(0x80000000), Color(0x80000000)],
                          ).createShader(bounds);
                        },
                        blendMode: BlendMode.darken,
                        child: Container(
                          decoration: BoxDecoration(
                            image: DecorationImage(
                              image: AssetImage('lib/common/images/welcomeBook2.jpg'),
                              fit: BoxFit.cover,
                            ),
                            boxShadow: [
                              BoxShadow(
                                color: Colors.black12,
                                blurRadius: 5,
                                spreadRadius: 2,
                              ),
                            ],
                          ),
                        ),
                      )
                          : Container(
                        decoration: BoxDecoration(
                          image: DecorationImage(
                            image: AssetImage('lib/common/images/welcomeBook2.jpg'),
                            fit: BoxFit.cover,
                          ),
                          boxShadow: [
                            BoxShadow(
                              color: Colors.black12,
                              blurRadius: 5,
                              spreadRadius: 2,
                            ),
                          ],
                        ),
                      ),
                    ),
                    ),
                  ),
              );
            },
          ),
          AnimatedBuilder(
            animation: _animation,
            builder: (context, child) {
              return Positioned(
                bottom: 500 - 200 * _animation.value,
                right: -320 + 160 * _animation.value,
                child: ConstrainedBox(
                  constraints: BoxConstraints(
                    maxWidth: 300,
                    maxHeight: 400,
                  ),
                  child: FractionallySizedBox(
                    widthFactor: 0.65,
                    heightFactor: 0.65,
                    child: Transform.rotate(
                      angle: -math.pi / 6,
                      child: isDarkMode
                          ? ShaderMask(
                        shaderCallback: (Rect bounds) {
                          return LinearGradient(
                            colors: [Color(0x80000000), Color(0x80000000)],
                          ).createShader(bounds);
                        },
                        blendMode: BlendMode.darken,
                        child: Container(
                          decoration: BoxDecoration(
                            image: DecorationImage(
                              image: AssetImage('lib/common/images/welcomeBook3.jpg'),
                              fit: BoxFit.cover,
                            ),
                            boxShadow: [
                              BoxShadow(
                                color: Colors.black12,
                                blurRadius: 5,
                                spreadRadius: 2,
                              ),
                            ],
                          ),
                        ),
                      )
                          : Container(
                        decoration: BoxDecoration(
                          image: DecorationImage(
                            image: AssetImage('lib/common/images/welcomeBook3.jpg'),
                            fit: BoxFit.cover,
                          ),
                          boxShadow: [
                            BoxShadow(
                              color: Colors.black12,
                              blurRadius: 5,
                              spreadRadius: 2,
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
              );
            },
          ),
          // 粉色心形图标
          Positioned(
            top: 140,
            left: 66,
            child: Icon(
              Icons.favorite,
              color: isDarkMode?Color(0x80ff4d6d): Color(0xFFff4d6d),
              size: 45,
            ),
          ),
          // 泡泡的图标
          Positioned(
            top: 145,
            right: 70,
            child: Icon(
              Icons.bubble_chart_outlined,
              color: Colors.grey[500],
              size: 40,
            ),
          ),
          // 主要内容
          Padding(
            padding: const EdgeInsets.only(
              top: 160,
              left: 30,
              right: 30,
            ),
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Align(
                    alignment: Alignment.centerLeft,
                    child: Text.rich(
                      TextSpan(
                        style: TextStyle(
                          fontSize: 58,
                          fontWeight: FontWeight.w100,
                          color: Colors.grey,
                          height: 1.24,
                        ),
                        children: <TextSpan>[
                          TextSpan(text: 'Thousand of\n'),
                          TextSpan(text: 'literary\n'),
                          TextSpan(text: 'treasures\n'),
                          TextSpan(text: 'await your\n'),
                          TextSpan(text: 'discovery'),
                        ],
                      ),
                      textAlign: TextAlign.left,
                    ),
                  ),
                  SizedBox(height: 3),
                  Text(
                    "Find your soul by exploring our thousands of collections from our library.",
                    style: TextStyle(
                      fontSize: 15.6,
                      fontWeight: FontWeight.bold,
                      color: isDarkMode ?Colors.grey : Colors.black54,
                    ),
                    textAlign: TextAlign.left,
                  ),
                  SizedBox(height: 27),
                  // 登录按钮
                  FractionallySizedBox(
                    widthFactor: 1.0,
                    child: SizedBox(
                      height: 50,
                      child: ElevatedButton(
                        style: ElevatedButton.styleFrom(
                          backgroundColor: isDarkMode?Color(0x80fe2566):Color(0xfffe2566),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(buttonBorderRadius),
                          ),
                        ),
                        onPressed:() => _showLoginDialog(isDarkMode),
                        child: Text(
                          "登录",
                          style: TextStyle(
                            fontSize: buttonTextFontSize,
                            fontWeight: FontWeight.bold,
                            color: Colors.black,
                          ),
                        ),
                      ),
                    ),
                  ),
                  SizedBox(height: 18),
                  // 跳过按钮
                  FractionallySizedBox(
                    widthFactor: 1.0,
                    child: SizedBox(
                      height: 50,
                      child: ElevatedButton(
                        style: ElevatedButton.styleFrom(
                          backgroundColor: isDarkMode?Color(0x80FFFFFF):Colors.white,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(buttonBorderRadius),
                          ),
                          side: BorderSide(color: Colors.black, width: 1),
                        ),
                        onPressed: () => context.push("/home"),
                        child: Text(
                          "跳过",
                          style: TextStyle(
                            fontSize: buttonTextFontSize,
                            fontWeight: FontWeight.bold,
                            color: Colors.black,
                          ),
                        ),
                      ),
                    ),
                  ),
                  SizedBox(height: 40),
                  // 注册
                  MouseRegion(
                    onEnter: _onEnter,
                    onExit: _onExit,
                    child: GestureDetector(
                      onTap:() => _showRegisterDialog(isDarkMode),
                      child: Text(
                      "没有账号？ 立即注册",
                        style: TextStyle(
                          fontSize: _isHovered ? 17 : 15,
                          fontWeight: _isHovered ? FontWeight.bold : FontWeight.normal,
                          color: _isHovered ?Colors.black54 : Colors.grey,
                        ),
                      ),
                    ),
                  ),
                  SizedBox(height: 40),
                  // 版权信息
                  Text(
                    "My Ebook Reader. All Rights Reserved.",
                    style: TextStyle(
                      fontSize: copyrightFontSize,
                      color: Colors.grey,
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}


